﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Xml.Linq;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Entity.ViewModels;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using Newtonsoft.Json;

namespace JinfuyuanProject.Controllers
{/*
  * 
  * 2025-04-19
  * 赵莹
  * 角色页面
  * 
  */
    public class RoleController : Controller
    {
        // GET: Role
        JinfuyuanDbContext db=new JinfuyuanDbContext();
        private readonly IRoleService _roleService;
        private readonly IModulesService _modulesService;
        private readonly IPagesService _pagesService;
        private readonly IRoleAndPageService _roleAndPageService;

        public RoleController(IRoleService roleService,IModulesService modulesService,IPagesService pagesService,IRoleAndPageService roleAndPageService)
        {
            _roleService = roleService;
            _modulesService=modulesService;
            _pagesService= pagesService;
            _roleAndPageService=roleAndPageService;
        }
        #region 视图页面
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult RoleList()
        {
            return View();
        }
        public ActionResult Add(int id=0)
        {
            var result = _roleService.GetModulePages(id);
            // 如果需要可以传递给ViewBag
            ViewBag.Modules = result.Modules;
            ViewBag.Pages = result.Pages;
            ViewBag.Id = id;
            return View();
        }
        #endregion

        #region 接口数据
        public JsonResult RoleContent(int page = 1,int limit = 10)
        {
            RoleFilter filter = new RoleFilter();
            filter.page = page;
            filter.limit = limit;
            filter.Isdelete = 0;
            
            // 返回数据给前端
            return Json(_roleService.Query(filter), JsonRequestBehavior.AllowGet);
        }
        //删除数据
        public JsonResult Delete(int Id)
        {
            Role role= _roleService.GetModel(Id);
            _roleService.Delete(role);
            Hashtable ht = new Hashtable();
            ht["code"] = 0;
            ht["msg"] = "success";
            return Json("ht", JsonRequestBehavior.AllowGet);
        }
        //权限设置页面添加角色数据
        [HttpPost]
        public JsonResult GetRole(int Id)
        {
            Hashtable ht = new Hashtable();
            Role role = _roleService.GetModel(Id);
            if (role == null)
                ht["code"] = 1;
            ht["code"] = 0;
            ht["data"] = role;
            return Json(ht);
        }
        //添加角色到数据库
        public JsonResult Insert(string productData, string selectedPageIds)
        {
            productData = HttpUtility.UrlDecode(productData);
            Role role = Newtonsoft.Json.JsonConvert.DeserializeObject<Role>(productData);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<int> PageIds = serializer.Deserialize<List<int>>(selectedPageIds);
            return Json(_roleService.RoleAndPageInsert(role, PageIds), JsonRequestBehavior.AllowGet);
        }
        //更新
        public JsonResult Update(string productData, string selectedPageIds)
        {
            productData = HttpUtility.UrlDecode(productData);
            Role role = Newtonsoft.Json.JsonConvert.DeserializeObject<Role>(productData);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<int> PageIds = serializer.Deserialize<List<int>>(selectedPageIds);
            return Json(_roleService.RoleAndPageUpdate(role,PageIds), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
